import { requestClient } from '#/api/request';

export namespace SystemUserApi {
  export interface SystemUser {
    [key: string]: any;
    id: string;
    username: string;
    realName: string;
    email?: string;
    phone?: string;
    status: 0 | 1;
  }
}

export async function getUserList(params?: Record<string, any>) {
  const res = await requestClient.get('/system/user/list', { params });
  if (res && Array.isArray(res.items)) {
    return {
      items: res.items.map((item: any) => ({
        ...item,
        remark: item.remark ?? '',
      })),
      total: res.total ?? res.items.length,
    };
  }
  return {
    items: [],
    total: 0,
  };
}

/**
 * 获取用户角色列表
 * @param userId 用户ID
 */
export async function getUserRoleList(userId: string) {
  return requestClient.get<Array<{ id: string; name: string }>>(`/system/user/${userId}/roles`);
}

export async function createUser(data: Omit<SystemUserApi.SystemUser, 'id'>) {
  return requestClient.post('/system/user', data);
}

export async function updateUser(id: string, data: Omit<SystemUserApi.SystemUser, 'id'>) {
  return requestClient.put(`/system/user/${id}`, data);
}

export async function deleteUser(id: string) {
  return requestClient.delete(`/system/user/${id}`);
}



export async function uploadFile(file: File, uploadUserId: string) {
  const formData = new FormData();
  formData.append('file', file); // file为File对象
  formData.append('uploadUserId', uploadUserId); // 新增上传用户ID
  return await requestClient.post('/common/upload', formData); // 不要加headers
}
